package com.basic.datapersist;

import java.util.Date;
import java.util.HashMap;

import org.apache.log4j.Logger;

import com.basic.common.FuncDate;
import com.basic.common.FuncStatic;

public class CatchOneRowData {
	private static Logger logger = Logger.getLogger ( CatchOneRowData.class) ;
	private HashMap<String, String> cacheData = new HashMap<String, String>();
	private HashMap<String, String> newData = new HashMap<String, String>();
	
	
	/**
	 * @param columnName
	 * @param value
	 */
	public void setValue(String columnName, String value) {
		newData.put(columnName, value);
		cacheData.put(columnName, value);
	}

	/**
	 * @param columnName
	 * @param value
	 */
	public void setValue(String columnName, Date value) {
		String date = FuncDate.dateToString(value);
		newData.put(columnName, date);
		cacheData.put(columnName, date);
	}

	/** @param column */
	public String getValue(String column) {
		return cacheData.get(column);
	}

	/** @param columnName */
	public Date getValueDate(String columnName) {
		String date = this.getValue(columnName);
		try {
			return FuncDate.formatToDate(date);
		} catch (Exception e) {
			logger.error(FuncStatic.errorTrace(e));
			return null;
		}
	}

	/** @param columnName */
	public Integer getValueInt(String columnName) {
		String value = this.getValue(columnName);
		try {
			return Integer.parseInt(value) ;
		} catch (Exception e) {
			logger.error(FuncStatic.errorTrace(e));
			return null;
		}
	}

	/** @param columnName */
	public Character getValueChar(String columnName) {
		String value = this.getValue(columnName);
		try {
			return value.charAt(0) ;
		} catch (Exception e) {
			logger.error(FuncStatic.errorTrace(e));
			return null;
		}
	}

	public HashMap<String, String> getNewData() {
		return this.newData;
	}

	/** @param data */
	public void putData(HashMap<String, String> data) {
		this.cacheData = data;
	}

	/**
	 * @param columnName
	 * @param value
	 */
	public void setCacheValue(String columnName, String value) {
		cacheData.put(columnName, value);
	}
	
	public void printData(){
		System.out.println("=============================");
		for(String column : this.cacheData.keySet()){
			System.out.println(column+":"+this.cacheData.get(column));
		}
	}
	
	public void printNewData(){
		System.out.println("=============================");
		for(String column : this.newData.keySet()){
			System.out.println(column+":"+this.newData.get(column));
		}
	}
}